package com.sharemarking.system.mapper;

import java.util.List;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.sharemarking.system.entity.Role;
import com.sharemarking.system.service.impl.MybatisCacheFactoryEhcacheImpl;

@CacheNamespace(implementation=MybatisCacheFactoryEhcacheImpl.class)
public interface RoleMapper {
	@Select("SELECT * FROM sys_role WHERE id=#{id}")
	Role get(Integer id);
	@Select("SELECT * FROM sys_role")
	List<Role> getAll();
	@Select("SELECT permission_id FROM sys_role_permission WHERE role_id=#{roleId}")
	List<Integer> getPermissionIdsByRoleId(Integer roleId);
	@Insert("INSERT INTO sys_role(name,enabled) VALUES (#{name},#{enabled})")
	@Options(useGeneratedKeys=true, keyProperty="id",keyColumn = "id")
	void insert(Role role);
	@Insert("INSERT INTO sys_role_permission VALUEs (#{roleId},#{permissionId}) ")
	void insertRolePermission(@Param("roleId")Integer roleId,@Param("permissionId")Integer permissionId);
	@Update("UPDATE sys_role SET name=#{name},enabled=#{enabled} WHERE id=#{id}")
	void updateInfo(Role role);
	@Delete("DELETE FROM sys_role_permission WHERE role_id=#{roleId} AND permission_id=#{permissionId}")
	void deletePermission(@Param("roleId")Integer roleId,@Param("permissionId")Integer permissionId);
	@Delete("DELETE FROM sys_role WHERE id=#{roleId}")
	void delete(Integer roleId);
	@Delete("DELETE FROM sys_role_permission WHERE role_id=#{roleId}")
	void deletePermissionByRoleId(@Param("roleId")Integer roleId);
}
